package com.wenyu.wenyuyuedu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.wenyu.wenyuyuedu.R;

import java.util.ArrayList;

/*
* 引导页
* */
public class GuideActivity extends AppCompatActivity implements View.OnClickListener {
    protected static final String TAG = GuideActivity.class.getSimpleName();
    private ViewPager mViewPager;
    private ArrayList<ImageView> mImageList;// 引导页的ImageView集合
    private int mPointWidth; // 两点间距
    private TextView btnStart;// 开始体验
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        initViews();
    }
    /**
     * 初始化界面
     */
    private void initViews() {
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        btnStart = (TextView) findViewById(R.id.btn_start);
        btnStart.setOnClickListener(this);

        initData();
        mViewPager.setAdapter(new GuideAdapter());
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            // 页面选中
            @Override
            public void onPageSelected(int position) {
                // Log.d(TAG, "onPageSelected:" + position);
                if (position == mImageList.size() - 1) {
                    btnStart.setVisibility(View.VISIBLE);
                } else {
                    btnStart.setVisibility(View.GONE);
                }
            }

            /**
             * 页面滑动监听
             *
             * @params position 当前选中的位置
             * @params positionOffset 偏移百分比
             * @params positionOffsetPixels 页面偏移长度
             */
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                int leftMargin = (int) (mPointWidth * (positionOffset + position));
                // Log.d(TAG, "当前位置:" + position + ";偏移比例:" + positionOffset
                // + ";点偏移:" + leftMargin);
            }
            // 状态改变
            @Override
            public void onPageScrollStateChanged(int state) {
                // Log.d(TAG, "onPageScrollStateChanged:" + state);
            }
        });
    }

    // 初始化ViewPager的数据
    private void initData() {
        int[] imageResIDs = {R.drawable.first, R.drawable.second,
                R.drawable.third};

        mImageList = new ArrayList<ImageView>();
        for (int i = 0; i < imageResIDs.length; i++) {
            ImageView image = new ImageView(this);
            image.setBackgroundResource(imageResIDs[i]);// 注意设置背景, 才可以填充屏幕
            mImageList.add(image);
        }
    }

    class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mImageList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mImageList.get(position));
            return mImageList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((ImageView) object);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                // 开始体验
               /* PrefUtils.putBoolean(this,
                        SplashActivity.PREF_IS_USER_GUIDE_SHOWED, true);// 记录已经展现过了新手引导页
*/
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);

                finish();

                break;

            default:
                break;
        }
    }
}
